/*
 * Copyright ©2018 贾维程. All rights reserved.
 */

package com.rtmart.imageupload.controller.supplier;
import com.rtmart.imageupload.Tools.FileTools;
import com.rtmart.imageupload.Tools.DateTools;
import com.rtmart.imageupload.Tools.IntegerTools;
import com.rtmart.imageupload.entity.Supplier;
import com.rtmart.imageupload.repository.SupplierRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/supplier/")
@PreAuthorize("hasAnyRole('ROLE_root','ROLE_admin')")
public class SupplierImportController {
    @Autowired
    private SupplierRepository supplierRepository;

    @GetMapping("import")
    public String getImport(){
        return "supplier/import";
    }

    @PostMapping("import")
    @Transactional
    public String itemImport(@RequestParam("file") MultipartFile file, Model model){
        if (file.isEmpty()) {
            model.addAttribute("errorMessage","文件是空的！");
            return "supplier/import";
        }
        List<String[]> csvList;
        if (FileTools.isCsv(file)) {
            csvList = FileTools.csvRowList(file);
        } else {
            model.addAttribute("errorMessage","文件不是CSV文件！");
            return "supplier/import";
        }

        List<Supplier> supplierList = new ArrayList<>();
        for(String[] supplierArray : csvList){
            Supplier supplier = new Supplier();
            String supplierName = supplierArray[0].trim();
            if (supplierName.indexOf('/') != -1 ||
                    supplierName.indexOf('\\') != -1 ||
                    supplierName.indexOf(':') != -1 ||
                    supplierName.indexOf('\"') != -1 ||
                    supplierName.indexOf('<') != -1 ||
                    supplierName.indexOf('>') != -1 ||
                    supplierName.indexOf('|') != -1 ||
                    supplierName.indexOf('?') != -1){
                model.addAttribute("errorMessage","厂商：" +supplierName+ ",不符合要求,厂商名不能含有 / \\ : * \" < > | ?");
                return "supplier/import";
            }
            supplier.setName(supplierName);
            supplier.setLastDate(DateTools.stringToDate(supplierArray[1].trim()));
            supplier.setState(IntegerTools.stringToInteger(supplierArray[2].trim()));
            supplierList.add(supplier);
        }
        supplierRepository.deleteAllInBatch();
        supplierRepository.saveAll(supplierList);
        model.addAttribute("message","导入成功");

        return "supplier/import";
    }



}
